package de.rj.urwalking.db;

import java.io.Serializable;

import org.simpleframework.xml.Element;
import org.simpleframework.xml.Root;

import com.google.android.gms.maps.model.LatLng;
import com.threed.jpct.SimpleVector;

import de.ralf.urwalking.soplugin.EPSGPoint;
import de.rj.urwalking.util.GeoUtil;

@Root(strict = false)
public class DBSalientObject implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	@Element(required = false)
	private Integer refpoi;
	@Element(required = false)
	private Integer refgebid;
	@Element(required = false)
	private Integer refindoor;
	@Element(required = false)
	private Double x;
	@Element(required = false)
	private Double y;
	private String name;
	@Element(required = false)
	private Integer uniqueid;
	@Element(name = "isindoor", required = false)
	private boolean isIndoor = false;
	private boolean isShown;

	private LatLng position;

	public DBSalientObject(Integer uniqueid, Integer refpoi, Integer refgebid, Integer refindoor, Double x, Double y,
			String name) {
		this.uniqueid = uniqueid;
		this.refpoi = refpoi;
		this.refgebid = refgebid;
		this.refindoor = refindoor;
		this.x = x;
		this.y = y;
		this.name = name;
	}

	public DBSalientObject(Double x, Double y, boolean isIndoor) {
		this.x = x;
		this.y = y;
		this.isIndoor = isIndoor;
	}

	public DBSalientObject(LatLng location, boolean isIndoor) {

		EPSGPoint epsgPoint = GeoUtil.convertLatLonToEPSG31468(location.latitude, location.longitude);

		this.x = epsgPoint.getX();
		this.y = epsgPoint.getY();
		this.isIndoor = isIndoor;
	}

	public boolean isIndoor() {
		return isIndoor;
	}

	public void setIndoor(boolean isIndoor) {
		this.isIndoor = isIndoor;
	}

	public Integer getRefindoor() {
		return refindoor;
	}

	public Integer getRefpoi() {
		return refpoi;
	}

	public Integer getRefgebid() {
		return refgebid;
	}

	public Double getX() {
		return x;
	}

	public Double getY() {
		return y;
	}

	public String getName() {
		return name;
	}

	public Integer getId() {
		return uniqueid;
	}

	public void applyOffset(SimpleVector offset) {

		if (x != 0.0 && y != 0.0) {
			x -= offset.x;
			y -= offset.y;
		}

	}

	public void setName(String name) {
		this.name = name;
	}

	public boolean isShown() {
		return isShown;
	}

	public void setIsShown(boolean isShown) {
		this.isShown = isShown;
	}

	public void calculateGpsCoords() {
		position = GeoUtil.convertEPSG31468toLatLon(y, x);
	}
	
	public LatLng getPosition() {
		return position;
	}
	

	@Override
	public String toString() {
		return name;
	}

}
